const express = require('express');
//创建路由对象
const pet = express.Router();
//引入数据库
const pool = require('../pool/pool');
const Joi = require('joi');
const Response = require('../utils/Response.js');
const { number } = require('joi');
// 1.  前台** 新增宠物接口
pet.post('/addPet', (req, res, next) => {
  let obj = req.body;
  let p_img = obj.pImg;
  let p_name = obj.pName;
  let p_year = obj.pYear;
  let p_gender = obj.pGender;
  let p_type = obj.pType;
  let p_content = obj.pContent;
  let p_breed = obj.pBreed;
  let user_id = obj.userId;
  let sql = `INSERT INTO pet value (?,?,?,?,?,?,?,?,?)`;
  pool.query(sql, [, p_img, p_name, p_year, p_gender, p_type, p_content, p_breed, user_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '宠物添加成功!' });
    } else {
      res.send({ code: '201', msg: '宠物添加失败!' });
      return;
    }
  });
});

// 2.前台** 查询宠物接口   根据用户id查询 某个用户 的宠物 接口

pet.post('/selectPet', (req, res, next) => {
  let obj = req.body;
  let user_id = obj.user_id;
  let sql = `select * from pet where user_id=? ORDER BY p_id`;
  pool.query(sql, [user_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    console.log(data);
    if (data.length === 0) {
      res.send({ code: 201, msg: '该用户没有宠物' });
    } else {
      res.send({ code: 200, msg: '查询宠物成功', data: data });
    }
  });
});

// 3.前台** 查询宠物接口   根据宠物id查询 某个宠物的具体信息  接口

pet.post('/selectPet/pet', (req, res, next) => {
  let obj = req.body;
  let p_id = obj.pet_id.split(',');
  let sql = `select * from pet where p_id in (?)`;
  pool.query(sql, [p_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    console.log(data);
    if (data.length === 0) {
      res.send({ code: 201, msg: '查询失败' });
    } else {
      res.send({ code: 200, msg: '查询宠物成功', data: data });
    }
  });
});

// 4 修改宠物信息接口
pet.put('/changePetInfo', (req, res, next) => {
  let obj = req.body;
  let p_id = obj.p_id;
  let p_img = obj.pImg;
  let p_name = obj.pName;
  let p_year = obj.pYear;
  let p_gender = obj.pGender;
  let p_type = obj.pType;
  let p_content = obj.pContent;
  let p_breed = obj.pBreed;
  let user_id = obj.user_id;

  let sql = `UPDATE pet SET p_img="${p_img}",p_name="${p_name}",p_year="${p_year}",p_content="${p_content}",p_breed="${p_breed}" WHERE p_id="${p_id}"`;
  pool.query(sql, [p_img, p_name, p_year, p_content, p_breed, p_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '修改宠物信息成功!' });
    } else {
      res.send({ code: '201', msg: '修改失败!' });
      return;
    }
  });
});

//5 删除宠物信息
pet.post('/delete', (req, res, next) => {
  let p_id = req.body.p_id;
  let sql = `DELETE FROM pet WHERE p_id=${p_id}`;
  pool.query(sql, [p_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '删除宠物成功!' });
    } else {
      res.send({ code: '201', msg: '删除失败!' });
      return;
    }
  });
});

//导出
module.exports = pet;
